package fr.ifsic.film.client.ui;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;

import fr.ifsic.film.client.FilmProject;
import fr.ifsic.film.client.dto.ActeurDTO;
import fr.ifsic.film.client.dto.PersonneDTO;

public class NouvelActeur extends NouveauType {

	ListBox listBox;
	TextBox nb;
	PersonneDTO[] pdto;

	public NouvelActeur() {
		super("Entrez un acteur");

		Grid grid = new Grid(2, 2);
		verticalPanel.add(grid);

		Label lblNewLabel_2 = new Label("Personne");
		grid.setWidget(0, 0, lblNewLabel_2);

		listBox = new ListBox();
		grid.setWidget(0, 1, listBox);

		Label lblNewLabel_3 = new Label("Nombre Film");
		grid.setWidget(1, 0, lblNewLabel_3);

		nb = new TextBox();
		grid.setWidget(1, 1, nb);

		horizontalPanel.add(confirmer);
		horizontalPanel.add(annuler);

	}

	public void refreshListe() {
		FilmProject.greetingService
				.listPersonne(new AsyncCallback<PersonneDTO[]>() {

					@Override
					public void onFailure(Throwable caught) {
					}

					@Override
					public void onSuccess(PersonneDTO[] result) {
						while (listBox.getItemCount() > 0)
							listBox.removeItem(0);

						pdto = result;
						for (PersonneDTO p : result) {
							listBox.addItem(p.getNom() + " " + p.getPrenom());
						}

					}
				});
	}

	protected void confirmer() {
		FilmProject.changePage(FilmProject.accueil);
		ActeurDTO adto = new ActeurDTO(pdto[listBox.getSelectedIndex()],
				nb.getText());
		FilmProject.greetingService.addA(adto, new AsyncCallback<Integer>() {

			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(Integer result) {
			}
		});

		super.confirmer();
	}

}
